package cn.bdqn.service.impl;

import cn.bdqn.bean.result.Page;
import cn.bdqn.domain.Storagedan;
import cn.bdqn.mapper.StoragedanMapper;
import cn.bdqn.service.StoragedanService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service(value = "StoragedanService")
public class StoragedanServiceImpl implements StoragedanService {

    @Autowired
    private StoragedanMapper storagedanMapper;

    @Override
    public Page<Storagedan> queryAll( Integer startNum) {
        Integer pageSize = 1;
        //开启分页 startNum > 0 ? startNum:1 判断当前页数是否大于0
        PageHelper.startPage(startNum != null ? startNum:1,pageSize);
        List<Storagedan> storagedans= storagedanMapper.selectAll();
        //分页
        PageInfo<Storagedan> storagedanPageInfo = new PageInfo<Storagedan>(storagedans);
        //封装分页数据
        Page page = new Page();
        //分页之后的数据
        page.setMenuList(storagedanPageInfo.getList());
        //总记录数
        page.setTotalRecord((int)storagedanPageInfo.getTotal());
        //返回封装的分页对象
        return page;
    }

    @Override
    public Page<Storagedan> queryByStoragedan(Integer startNum,Storagedan storagedan) {

        Page page=new Page();
        //判断当前是第几页,如果startNum为空
        PageHelper.startPage(startNum != null ? startNum:1,page.getPageSize());
        List<Storagedan> storagedans= storagedanMapper.selectByStoragedant(storagedan);

        //分页
        PageInfo<Storagedan> pageInfo=new PageInfo<Storagedan>(storagedans);
        //获取分页之后的数据
        page.setMenuList(pageInfo.getList());
        //总记录数
        page.setTotalRecord((int)pageInfo.getTotal());
        System.out.println(page);
        //返回分页对象
        return  page;
    }

    @Override
    public void save(Storagedan storagedan) {
        storagedanMapper.insertSelective(storagedan);
    }

    @Override
    public void update(Storagedan storagedan) {
        storagedanMapper.updateByPrimaryKeySelective(storagedan);
    }

    /*
    删除药品信息
    用于药品因为过期不可用进行删除
     */
    @Override
    public void delete(Integer id) {
        storagedanMapper.deleteByPrimaryKey(id);
    }

    /*
     查询药品的有效状态
     */
    @Override
    public List<Storagedan> queryByEffectiveStatus(Integer effectiveStatus) {
        return storagedanMapper.selectByEffectiveStatus(effectiveStatus);
    }
    /*
     查询库存紧张的药品信息
     */
    @Override
    public List<Integer> queryIDSByDanStorageNumber() {
        return storagedanMapper.selectIDSByDanStorageNumber();
    }

    /**
     * 改变药品数量
     */
    @Override
    public String updateDanStorageNumber(String danName, Integer number) {
        //创建一个bool值 用于while 循环
        boolean b=false;
        //首先判断该药品的数量是否多于要申请的数量
        Integer num= storagedanMapper.selectSumNumberByDanName(danName);

        if (num<number){
            return danName+"数量不足";
        }else {
            //循环判断在药库中当前药品时间日期在前的那批药品是否充足 如果不充足循环查找下一批
            do {
                Storagedan storagedan= storagedanMapper.selectByDanName(danName);
                if (storagedan.getDanStoragenumber()<number){
                    storagedan.setDanStoragenumber(0);
                    storagedanMapper.updateByPrimaryKeySelective(storagedan);
                    b=true;
                }else {
                    storagedanMapper.updateDanStorageNumber(danName,number);
                    b=false;
                }
            }while (b);
            return danName+"药品充足";
        }
    }
}
